<?php
    class Default_Model_News extends Zend_Db_Table_Abstract
    {
        protected $_name = 'news';
        protected $_primary = 'news_id';
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listNews($cat_id, $count = null, $offset = null)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('cat_id = ?', $cat_id)
                            ->where('status = 1')
                            ->order('news_id DESC')
                            ->limit($count, $offset);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function listNewsByCategory($cat_id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('cat_id = ?', $cat_id)
                            ->where('status = 1')
                            ->order('news_id DESC');
            return $sql;
        }
        
        public function getNewsById($news_id)
        {
            $news = array(
                'news_id',
                'cat_id',
                'news_title',
                'news_image',
                'news_content',
                'news_plaintext',
                'create_date',
                'DAYNAME(create_date) as day',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $news)
                            ->where('news_id = ?', $news_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function searchNews($keyword)
        {
            $where = "MATCH(news_title, news_plaintext) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where($where);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function convertDayOfWeek($day)
        {
            switch($day)
            {
                case    'Monday'    : $result = 'Thứ Hai'; break;
                case    'Tuesday'   : $result = 'Thứ Ba'; break;
                case    'Wednesday' : $result = 'Thứ Tư'; break;
                case    'Thursday'  : $result = 'Thứ Năm'; break;
                case    'Friday'    : $result = 'Thứ Sáu'; break;
                case    'Saturday'  : $result = 'Thứ Bảy'; break;
                case    'Sunday'    : $result = 'Chủ Nhật'; break;
            }
            return $result;
        }
    }
